﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using FBEntities.FBObjects.User;
using System.Windows.Forms;
using System.Drawing;

namespace XFControls.XFPanels.XFPanelItems
{
    public enum XFItemProfileHeadSelectedType
    {
        Picture,
        Status
    }

    public class XFItemProfileHead : IXFItemReceiveClick
    {
        private int _imageHeight = 128;
        private int _imageWidth = 128;
        private int _height;
        private string _status;
        private XFItemProfileHeadSelectedType _selected;
        private FBPerson _user;

        public FBPerson User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;

                if (value.id == XFItemControlUtils.Me.Me.id)
                    ItemType = XFItemType.Clickable;
                else
                    ItemType = XFItemType.Display;

                _user.Updated += (o, e) =>
                {
                    ResetHeight();

                    if (Buffer != null)
                        Buffer.Dispose();
                    Buffer = null;

                    if (Parent.InvokeRequired && !Parent.IsDisposed)
                        Parent.Invoke(new Action(() => { Parent.Invalidate(); }));
                };
            }
        }

        #region IXFItem Members

        public XFItemType ItemType { get; set; }
        public XFPanelBase Parent { get; set; }
        public XFItemState State { get; set; }
        public XFItemStyle Style { get; set; }
        public Bitmap Buffer { get; set; }

        public XFItemProfileHead()
        {
            _height = -1;
            Style = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                Padding = 10,
                BackgroundColor = Color.RoyalBlue
            };

            ItemType = XFItemType.Display;
        }

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            if (User == null || User.Statuses.data.Count == 0 || string.IsNullOrEmpty(User.Statuses.data.First().message))
                return (_height = _imageHeight + Style.PaddingTop);

            using (Bitmap bm = new Bitmap(100, 100))
            {
                //int width = Screen.PrimaryScreen.WorkingArea.Width;
                int width = Screen.PrimaryScreen.WorkingArea.Width;

                if (Parent != null && !Parent.InvokeRequired)
                {
                    width = Parent.Width;
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }
                else if (Parent.InvokeRequired)
                {
                    Parent.Invoke(new Action(() =>
                        {
                            width = Parent.Width;
                            width -= Parent.AlphaBarWidth;

                            if (Parent.ScrollbarVisible)
                                width -= 5;
                        }));
                }

                width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;
                _status = User.Statuses.data.First().message;

                var gr = Graphics.FromImage(bm);
                _status = XFControlUtils.GetSizedString(width, _status, Style.TextFont);
                _height = (int)gr.MeasureString(_status, Style.TextFont).Height + Style.PaddingBottom;
                gr.Dispose();

            }

            if (_height < _imageHeight + Style.PaddingTop)
                _height = _imageHeight + Style.PaddingTop;

            _height += Style.PaddingBottom;
            return _height;
        }

        public void ResetHeight()
        {
            _height = -1;
        }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;

            XFControlUtils.DrawBackground(Style, g, x, y, width, _height);
            XFControlUtils.DrawBoarders(Style, g, x, y, width, _height);

            string url = _user.Picture.url + "&type=large";
            this.DrawImage(url, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _imageWidth, _imageHeight), true);

            if (Parent.InvokeRequired)
                Parent.Invoke(new Action(() =>
                {

                    ResetHeight();
                    if (Buffer != null)
                        Buffer.Dispose();
                    Buffer = null;

                    Parent.Reset();
                    Parent.Invalidate();
                }));

            using (Pen p = new Pen(Color.DarkGray))
                g.DrawLine(p, 0, y + _height, width, y + _height);
            using (SolidBrush b = new SolidBrush(Color.White))
            {
                if (Parent != null)
                {
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }

                width -= _imageWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;

                if (User == null || string.IsNullOrEmpty(_status))
                    g.DrawString("No status", Style.TextFont, b, x + (Style.PaddingLeft * 2) + _imageWidth, y + Style.PaddingRight);
                else
                    g.DrawString(_status, Style.TextFont, b, x + (Style.PaddingLeft * 2) + _imageWidth, y + Style.PaddingRight);
            }
        }

        public XFItemClickResult GetClickResult()
        {
            if (_selected == XFItemProfileHeadSelectedType.Status)
            {
                Parent.GetInputText();

                Parent.TextEntered += (o, e) =>
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Application.DoEvents();

                    if (User.UpdateStatus(e.TextEntered))
                    {
                        ResetHeight();
                    }

                    Cursor.Current = Cursors.Default;
                };
            }
            return XFItemClickResult.TextInput;
        }

        public void GetClick(int x, int y)
        {
            if (x < (Style.PaddingLeft * 2) + _imageWidth)
                _selected = XFItemProfileHeadSelectedType.Picture;
            else
                _selected = XFItemProfileHeadSelectedType.Status;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();

            Buffer = null;
            if (Style != null)
                Style.Dispose();

            Style = null;
        }

        #endregion
    }
}
